package cn.edu.sjtu.se.dslab.component.design.generate.element;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class PageElementParseUtil {
	private static final Logger logger = Logger
			.getLogger(PageElementParseUtil.class);

	public static PageElement parsePageElement(String s) {
		if (s == null || s.equals(""))
			return null;

		logger.info(s);
		String[] pairs = s.split(",");
		Map<String, String> elementMap = new HashMap<String, String>();
		for (String p : pairs) {
			String[] kv = p.split(":");
			elementMap.put(kv[0], kv[1]);
			//logger.info("put " + kv[0] + ":" + kv[1]);
		}
		String elementType = elementMap.get("element_type").toUpperCase();
		if (elementType == null || "".equals(elementType))
			return null;
		
		logger.info("Element Type:" + ElementType.valueOf(elementType));
		switch (ElementType.valueOf(elementType)) {
		case STATIC_STRING:
			return new StaticString(elementMap);
		case POINT:
			return new Point(elementMap);
		case DYNAMIC_VAR: 
			return new DynamicVar(elementMap);
		case LINE:
			return new Line(elementMap);
		case RECTANGLE:
			return new Rectangle(elementMap);
		default:
		}
		return null;
	}
	
}
